package com.czz.sms.service.impl;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jsms.api.SendSMSResult;
import cn.jsms.api.common.SMSClient;
import cn.jsms.api.common.model.SMSPayload;
import com.czz.common.exception.CustomerException;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.sms.service.SmsService;
import com.czz.sms.utils.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;

/**
 * @ClassName: SmsServiceImpl
 * @Author Czz
 * @Date 2020/2/20
 * @Time 10:36
 **/
@Service
public class SmsServiceImpl implements SmsService {

    @Override
    public void getUserSms(String phone,String code) {
        //获取极光短信常量
        String appKey = ConstantPropertiesUtil.APP_KEY;
        String masterSecret = ConstantPropertiesUtil.MASTER_SECRET;

        //构建连接器
        SMSClient smsClient = new SMSClient(masterSecret, appKey);

        //构建请求体
        SMSPayload payload = SMSPayload.newBuilder()
                .setMobileNumber(phone)
                .addTempPara("code", code)
                .setTempId(1)
                .build();
        //发送请求
        try {
            SendSMSResult res = smsClient.sendTemplateSMS(payload);
        } catch (APIConnectionException | APIRequestException e) {
            throw new CustomerException(ResultCodeEnum.SEND_SMS_ERROR);
        }
    }
}
